/*******************************************************************************
 * Copyright (c) 2008, 2009 Fabio Mauro - Alberto Terraneo.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 1.2
 * or any later version published by the Free Software Foundation.
 *     
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied 
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
 * See the GNU Free Documentation License for more details.
 *     
 * You should have received a copy of the GNU Free Documentation 
 * License along with this distribution; if not, send a email to authors:
 * aterraneo <at> users.sourceforge.net
 * bigmoby09 <at> users.sourceforge.net
 *******************************************************************************/
package it.blueocean.acanto.web.platform.components.tabs;

import it.blueocean.acanto.dao.NavigationItemDao;
import it.blueocean.acanto.model.NavigationItem;
import it.blueocean.acanto.web.AcantoPlatformApplication;
import it.blueocean.acanto.web.platform.components.tabs.tabpanel.components.DynamicTabs;
import it.blueocean.acanto.web.platform.components.tabs.tabpanel.container.CustomSecureTabbedPanel;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.wicket.Application;
import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.panel.Panel;

/**
 * Tabbed panel container.
 * 
 */
public class TabbedPanelContainer extends Panel 
{
	private static final long serialVersionUID = 7320211458187506640L;

	private final static Logger logger = Logger.getLogger(TabbedPanelContainer.class);

	public TabbedPanelContainer(final String id, final WebPage page)
	{
		super(id);

		PageParameters parameters = new PageParameters();
		parameters.put("pageId", page.getId());

		Panel tabBar = getTabBar("tabs");
		
		add(tabBar);
	}

	private Panel getTabBar(String id)
	{
		List<DynamicTabs> listTabs = getTabs();

		CustomSecureTabbedPanel secureTabbedPanel = new CustomSecureTabbedPanel(id, listTabs);

		return secureTabbedPanel;
	}

	private List<DynamicTabs> getTabs()
	{
		List<DynamicTabs> tabs = new ArrayList<DynamicTabs>();

		logger.debug("getting dao from SpringContext");

		NavigationItemDao navigationItemDao = (NavigationItemDao) ((AcantoPlatformApplication) Application.get()).getSpringContext()
			.getBean("navigationItemDao");

		Set<NavigationItem> orderedNavigationItems = navigationItemDao.getOrderedMenu();

		logger.debug("menu creation");

		for (NavigationItem navigationItem : orderedNavigationItems)
		{
			DynamicTabs tabPanel = new DynamicTabs(navigationItem);

			tabs.add(tabPanel);
		}

		return tabs;
	}

}
